import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/channelHelper.dart';
import '../../helper/downloadHelper.dart';
import '../../helper/routeHelper.dart';
import '../../model/station.dart';
import '../../model/stationcheck.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../widgets/someCards.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../helper/uidata.dart';
import '../../model/report.dart';
import 'station_helper.dart';

class StationCheckInfoPage extends StatefulWidget {
  @override
  StationCheckInfoPage(this.station1, this.stc1, this.added);
  final Station station1;
  final StationCheck stc1;
  final VoidCallback added;
  @override
  StationCheckInfoPageState createState() => new StationCheckInfoPageState();
}

class StationCheckInfoPageState extends State<StationCheckInfoPage> {
  late StationCheck X1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    X1 = widget.stc1;
  }

  loadData() async {
    var xx = await StationCheck.Get("ID=@ID", {"ID": widget.stc1.ID});
    if (xx!.ExamQM == null) xx.ExamQM = "";

    if (xx.RecordQM == null) xx.RecordQM = "";
    setState(() {
      X1 = xx;
    });
  }

  upload(BuildContext context) async {
    if (X1.ID == 0) {
      await StationCheck.Add(X1);
    } else {
      await StationCheck.Update(X1);
    }

    // widget.added();
    Navigator.of(context).pop();
  }

  void onRQMChange(String nn) async {
    String yy = nn.toString();

    //print(yy);
    setState(() {
      X1.RecordQM = yy;
    });

    await StationCheck.Update(X1);
  }

  void onEQMChange(String nn) async {
    String yy = nn.toString();

    //print(yy);
    setState(() {
      X1.ExamQM = yy;
    });
    await StationCheck.Update(X1);
  }

  Widget getFloatingButton(BuildContext context) {
    if (widget.stc1.UserId == UIData.CurrentUser!.Guid && X1.ExamQM == "") {
      return FloatingActionButton(
        child: Icon(FontAwesomeIcons.penAlt),
        onPressed: () =>
            RouteHelper.StationCheckAdd(context, widget.station1, X1, loadData),
      );
    }

    if (X1.ExamQM == null) X1.ExamQM = "";

    if (X1.ExamQM != "") {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.download),
          onPressed: () async {
            await DownloadHelper.DownLoadStationCheck(context, widget.stc1);
            await ChannelHelper.showToast("已下载，请到下载列表里查看");
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("检修记录"),
      ),
      body: StationCheckInfoBody(context),
      floatingActionButton: getFloatingButton(context),
    );
  }

  List<Widget> getAllWidget(BuildContext context) {
    if (X1.ExamQM == null) X1.ExamQM = "";

    if (X1.RecordQM == null) X1.RecordQM = "";

    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.CheckImage(),
        title: Text(X1.StationName),
        trailing: Text(Helper.DateTimeFormat(X1.CreateTime)),
      ),
    ));

    //list2.add(InputWidget.getInfo("设备名称",Helper.DateTimeFormat(X1.CreateTime)));
    //list2.add(Divider(height: 0.0,));
    list2.add(InputWidget.getInfo("外观是否正常", X1.WaiGuan));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("卫星发射是否正常", X1.WeiXing));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("短信发射是否正常", X1.DuanXin));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("蓄电池及太阳能面板电压值", X1.DianYa));
    list1.add(InputWidget.inCard2(list2));

    list3.add(InputWidget.getInfo("雨量初始值", X1.Rain1));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo("雨量模拟后值", X1.Rain2));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("工作情况", X1.Works));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("更换设备", X1.CEquips));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInfo2("遗留问题", X1.Problems));

    list1.add(InputWidget.inCard2(list3));

    list4.add(InputWidget.getInfo("巡查人员", X1.CMembers));
    list4.add(Divider(
      height: 0.0,
    ));

    if (widget.stc1.UserId == UIData.CurrentUser!.Guid && X1.RecordQM == "") {
      list4.add(
          InputWidget.getQMInput(context, "记录签字", X1.RecordQM, onRQMChange));
    } else {
      list4.add(InputWidget.getQMTile3("记录员", X1.RecordQM));
    }

    list4.add(Divider(
      height: 0.0,
    ));

    if (widget.stc1.UserId != UIData.CurrentUser!.Guid &&
        StationHelper.IfGuanLi(UIData.CurrentUser!) &&
        X1.RecordQM != "" &&
        X1.ExamQM == "") {
      list4
          .add(InputWidget.getQMInput(context, "审核签字", X1.ExamQM, onEQMChange));
    } else {
      list4.add(InputWidget.getQMTile3("审核人", X1.ExamQM));
    }

    list1.add(InputWidget.inCard2(list4));

    //list1.add(InputWidget.getQMTile(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  StationCheckInfoBody(BuildContext context) => SingleChildScrollView(
        child: StationCheckInfoFields(context),
      );

  StationCheckInfoFields(BuildContext context) {
    return SingleChildScrollView(
      child: new Container(
          margin: EdgeInsets.only(top: 4.0),
          child: new Column(children: getAllWidget(context)
              /*
            Card(
              child: (ListTile(
                title: Center(child: Text("查看地图")),
                onTap: () {
                  RouteHelper.MyMapPage(context, s1.LTTD, s1.LGTD);
                },
              )),
              color: GlobalConfig.cardBackgroundColor,
              margin: const EdgeInsets.all(6.0),
            ),*/
              )),
    );
  }
}
